import { IList, get, post } from "./index";
export interface UserQuery {
    userName: string;
    institutionId: number;
    roleType: number; // 1-管理员 2-职员
    startTime: string;
    endTime: string;
    pageNum: string;
    pageSize: string;
}

export interface User {
    userName: string;
    institutionName: string;
    roleType: number; // 1-管理员 2-职员
    createTime: string;
    userId: number;
}

export interface UserDetail {
    userName: string;
    userAccount: string;
    phone: string;
    email: string;
    roleType: number; // 1-管理员 2-职员
    institutionName: string;
}

interface UpdatePassword {
    originalPwd: string;
    newPwd: string;
}

export interface AddUserParams {
    userName: string;
    userAccount: string;
    phone?: string;
    email: string;
    institutionId: number;
    roleType: number;
}

export type EditUserParams = AddUserParams & {
    userId: number;
};

export const getUsers = async (params: UserQuery): Promise<IList<User>> => {
    const res = await post("/user/list", params);
    return {
        total: res.data?.total || 0,
        list: res.data?.userListDto || [],
    };
};

export const getUserDetail = async (userId: number): Promise<UserDetail> => {
    const res = await get("/user/detail", { userId });
    return res.data || {};
};

export const resetUserPassword = (userId: number) => post("/user/reset", { userId });

export const deleteUser = (userId: number) => post("/user/delete", { userId });

export const updatePassword = (data: UpdatePassword) => post("/individual/update/pwd", data);

export const addUser = (params: AddUserParams) => post("/user/add", params);

export const editUser = (params: EditUserParams) => post("/user/update", params);
